// Websocket Transport
// -------------------
// Define the transport to carry all realtime requests
// Uses 'engineio' by default. See README to see how to configure it
'use strict';

module.exports = function(ss, emitter) {
  var config = {}, transport;

  return {
    use: function(nameOrModule, cfg) {
      var modPath;
      if (cfg == null) {
        cfg = {};
      }
      config = cfg;
      transport = null;

      if (typeof nameOrModule === 'function') {
        return nameOrModule;
      } else {
        modPath = './transports/' + nameOrModule;
        if (require.resolve(modPath)) {
          return (transport = require(modPath));
        } else {
          throw new Error('Unable to find the \'' + nameOrModule + '\' websocket transport internally');
        }
      }
    },
    load: function(httpServer, sessionOptions) {
      if (transport == null) {
        this.use('engineio');
      }
      return transport(ss, emitter, httpServer, config, sessionOptions);
    }
  };
};
